﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for ColorPlateEditWindow.xaml
    /// </summary>
    public partial class ColorPlateEditWindow : Window
    {
        public ColorPlate RetColorPlate { get; private set; }

        private System.Collections.ObjectModel.ObservableCollection<ColorPlateModel> _colorList;
        public ColorPlateEditWindow(ColorPlate colorPlate)
        {
            InitializeComponent();

            _colorList = new System.Collections.ObjectModel.ObservableCollection<ColorPlateModel>();
            if (colorPlate != null && colorPlate.GetColorList() != null)
            {
                int index = 0;
                foreach (var item in colorPlate.GetColorList())
                {
                    ColorPlateModel colorPlateModel = new ColorPlateModel();
                    colorPlateModel.Index = ++index;
                    colorPlateModel.ColorString = (item as SolidColorBrush).ToString();

                    _colorList.Add(colorPlateModel);
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _ctlColorPlate.ItemsSource = _colorList;
        }

        private void DataGridSort()
        {
            ICollectionView v = CollectionViewSource.GetDefaultView(_ctlColorPlate.ItemsSource);
            v.SortDescriptions.Clear();
            v.SortDescriptions.Add(new SortDescription("Index", ListSortDirection.Ascending));
            v.Refresh();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RetColorPlate = new ColorPlate();
            foreach(var item in _colorList)
            {
                RetColorPlate.AddColor(item.ColorBrush);
            }
            DialogResult = true;
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void _ctlAdd_Click(object sender, RoutedEventArgs e)
        {
            ColorPlateModel colorPlateModel = new ColorPlateModel();
            colorPlateModel.Index = _colorList.Count + 1;
            colorPlateModel.ColorString = "#FFFFFFFF";

            _colorList.Add(colorPlateModel);
        }

        private void _ctlRemove_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlColorPlate.SelectedItem != null && _ctlColorPlate.SelectedIndex >= 0)
            {
                int curIndex = _ctlColorPlate.SelectedIndex;
                ColorPlateModel colorPlateModel = _ctlColorPlate.SelectedItem as ColorPlateModel;
                _colorList.Remove(colorPlateModel);
                for(int i = curIndex; i < _colorList.Count; i++)
                {
                    _colorList[i].Index--;
                }
                if (_colorList.Count > curIndex)
                {
                    _ctlColorPlate.SelectedIndex = curIndex;
                }
            }
        }

        private void _ctlUp_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlColorPlate.SelectedItem != null && _ctlColorPlate.SelectedIndex >= 0)
            {
                int curIndex = _ctlColorPlate.SelectedIndex;
                if(curIndex > 0)
                {
                    ColorPlateModel colorPlateModel = (ColorPlateModel)_ctlColorPlate.SelectedItem;
                    _colorList.ElementAt(curIndex).Index--;
                    _colorList.ElementAt(curIndex - 1).Index++;
                    _colorList.RemoveAt(curIndex);
                    _colorList.Insert(curIndex - 1, colorPlateModel);

                    _ctlColorPlate.SelectedIndex = curIndex - 1;
                }
            }
        }

        private void _ctlDown_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlColorPlate.SelectedItem != null && _ctlColorPlate.SelectedIndex >= 0)
            {
                int curIndex = _ctlColorPlate.SelectedIndex;
                if (curIndex < _colorList.Count - 1)
                {
                    ColorPlateModel colorPlateModel = (ColorPlateModel)_ctlColorPlate.SelectedItem;
                    _colorList.ElementAt(curIndex).Index++;
                    _colorList.ElementAt(curIndex + 1).Index--;
                    _colorList.RemoveAt(curIndex);
                    _colorList.Insert(curIndex + 1, colorPlateModel);

                    _ctlColorPlate.SelectedIndex = curIndex + 1;
                }
            }
        }
    }

    class ColorPlateModel : INotifyPropertyChanged
    {
        private int _Index;
        public int Index
        {
            get { return _Index; }
            set
            {
                _Index = value;
                NotifyPropertyChanged();
            }
        }

        private string _ColorString;
        public string ColorString
        {
            get { return _ColorString; }
            set
            {
                _ColorString = value;
                NotifyPropertyChanged();
                NotifyPropertyChanged("ColorBrush");
            }
        }
        public Brush ColorBrush
        {
            get
            {
                try
                {
                    return new SolidColorBrush((Color)ColorConverter.ConvertFromString(ColorString));
                }
                catch
                {
                    return new SolidColorBrush(Colors.Transparent);
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName]string propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
